package com.itheima.ExtendsClass;

import com.hzheima.game.GameJPanel;
import com.hzheima.util.ImageUtil;
import com.itheima.FinalCanstans;
import com.itheima.member.*;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class BallFightPanel extends GameJPanel {
    //在面板中创建一个空参构造方法,用于给初始变量赋值
    private int p1 = 0;
    private int p2 = 0;

    BufferedImage bg;
    BufferedImage bg1;
    BufferedImage money;
    BufferedImage st;
    BufferedImage ed;

    Plane plane;
    Plane2 plane2;
    Boss boss;

    ArrayList<Bullet> bullets;
    ArrayList<Bullet> bullets2;
    ArrayList<Bullet3> bullets3;
    ArrayList<bossBullet> bossBullets;

    ArrayList<Ball> balls;
    //创建类用来存储小星星
    ArrayList<Picture3> picture3s ;
    //判断飞机1是否被击中
    boolean gameOver;
    //判断飞机2是否被击中
    boolean gameOver2;
    //开启无敌功能
    boolean Continue;
    //切换单双人模式
    boolean paintFunction;
    //双人模式
    boolean paintFunction2;
    //控制红色矩形是否存在
    boolean rec ;
    //判断飞机1是否开始移动
    boolean l;
    boolean r;
    boolean u;
    boolean d;
    //判断飞机2是否开始移动
    boolean l2;
    boolean r2;
    boolean u2;
    boolean d2;
    //判断游戏是否暂停
    boolean ban;
    //判断小星星是否碰撞飞机
    boolean  stars;

    //创建变量记录击中小球获得的分数
    int sum = 0;
    int sum2 = 0;
    //创建变量控制进度
    int amount = 0;
    //创建变量记录移除多少小球的数量
    int sumBall = 0;
    int sumBall2 = 0;
    //创建变量记录历史最高记录
    int maxScores = 0;
    int maxScores2 = 0;
    //创建变量控制进度条
//    int num = 1;
    //创建变量控制关卡

    int checkpoint = 1;
    //创建关卡1背景图片对象
    Picture picture;
    //创建关卡2背景图片对象
    Picture2 picture2;

    public BallFightPanel() {
//        //调用方法在面板中添加背景图片
//        bg = ImageUtil.getImg("BG.png");
//        //换图背景
//        bg1 = ImageUtil.getImg("BG1.jpg");
        //添加金币图片

        //加载音乐方法
        playBGMusic();
        money = ImageUtil.getImg("money.png");
        //添加进度条左括号
        st = ImageUtil.getImg("st.png");
        //添加进度条右括号
        ed = ImageUtil.getImg("ed.png");
        //创建Boss对象
        boss = new Boss();
        plane = new Plane();
        //创建飞机1类
        plane2 = new Plane2();
        //调用飞机1类中创建的子弹集合
        bullets = plane.getBullets();
        //调用飞机1类中创建的子弹2集合
        bullets3 = plane.getBullets3();
        //调用飞机2类中创建的子弹集合
        bullets2 = plane2.getBullets();
        //调用Boss类中创建的子弹集合
        bossBullets = boss.getBossBullets();
        //创建小星星对象
        picture3s = new ArrayList<>();
        //创建球对象
        balls = new ArrayList<>();
        //创建关卡1背景图片对象
        picture = new Picture();
        //创建关卡2背景图片对象
        picture2 = new Picture2();
    }

    /**
     * 监控用户每次按键
     * 控制飞机的上下左右移动
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == 'A' && !gameOver) {
            l = true;
            r = false;
//            plane.moveToLeft();
        } else if (e.getKeyCode() == 'D' && !gameOver) {
            r = true;
            l = false;
//            plane.moveToRight();
        } else if (e.getKeyCode() == 'W' && !gameOver) {
            u = true;
            d = false;
//            plane.moveToUp();
        } else if (e.getKeyCode() == 'S' && !gameOver) {
            d = true;
            u = false;
//            plane.moveToDown();
        } else if (e.getKeyCode() == 37 && !gameOver2) {
            //40=下键，39=右键' 38=上键，37=左键
            l2 = true;
            r2 = false;
//            plane2.moveToLeft2();
        } else if (e.getKeyCode() == 39 && !gameOver2) {
            r2  = true;
            l2  = false;
//            plane2.moveToRight2();
        } else if (e.getKeyCode() == 38 && !gameOver2) {
            u2  = true;
            d2  = false;
//            plane2.moveToUp2();
        } else if (e.getKeyCode() == 40 && !gameOver2) {
            d2 = true;
            u2 = false;
//            plane2.moveToDown2();
        }else if (e.getKeyCode() == 'Z' ){
            paintFunction = true;
        }else if (e.getKeyCode() == 'X'){
            paintFunction2 = true;
        }
        else if (e.getKeyCode() == 112) {
            //重新开始游戏
            gameOver = false;
            gameOver2 = false;

            //比较得出历史最高分
            if (p1 > maxScores) {
                maxScores = p1;
            }
            if (p2 > maxScores2) {
                maxScores2 = p2;
            }
            //分数清空
            p1 = 0;
            p2 =0;
            //金币清空
            sumBall = 0;
            sumBall2 = 0;
            //关卡清空
            checkpoint = 1;
            //进度条清空
            amount = 0;
            //小球清空
            balls.clear();
            bossBullets.clear();
            //飞机重新绘制
            plane = new Plane();
            plane2 = new Plane2();
            //子弹重新绘制
            bullets = plane.getBullets();
            //按下F1之后boss的子弹重新绘制
//            boss.shootBullet0();
            //按下F1之后第二种的子弹重新绘制
            bullets3 = plane.getBullets3();
            //按下F1之后Boss的子弹重新绘制
            bossBullets = boss.getBossBullets();
//            //按下F1之后飞机2的子弹重新绘制
            bullets2 = plane2.getBullets();
//            //按下F1之后小星星重新绘制
//            picture3s.
        } else if (e.getKeyCode() == 113) {
            //无敌模式
            Continue = true;
        } else if (e.getKeyCode() == 114) {
            //退出无敌模式
            Continue = false;
        }else if(e.getKeyCode() == 115){
            //游戏暂停
            ban = true;
        }else if(e.getKeyCode() == 116){
            //游戏重开始
            ban = false;
        }

        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //绘制背景图片
//        g.drawImage(bg, 0, 0, FinalCanstans.APP_WIDTH, FinalCanstans.APP_HIGHT, null);
        picture.draw(g);
        if (checkpoint >= 2) {
            picture2.draw(g);
        }
        if(!rec){
            //绘制半透明长方体
            //设置画笔颜色
            g.setColor(Color.red);
            //设置透明度绘制红色矩形
            Graphics2D g2d = (Graphics2D) g;
            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
            g2d.setComposite(alphaComposite);
            g2d.fillRect(0, 100, FinalCanstans.APP_WIDTH, 400);
            //重新设置字体样式和颜色
            g.setColor(Color.white);
            g.setFont(new Font("楷体", Font.BOLD, 25));
            g.drawString("模式选择", 380, 200);
            g.drawString("单人模式", 380, 250);
            g.drawString("双人模式", 380, 300);
            g.drawString("按 Z 选择单人模式,按 X 选择双人模式", 300, 350);
        }
        // 单人模式
        if (paintFunction){
            rec = true;
            //设置字体样式
            g.setFont(new Font("楷体", Font.ITALIC, 25));
            //设置字体颜色
            g.setColor(Color.white);
            //绘制玩家1
            g.drawString("玩家:1",20,40);
            //绘制分数1
            g.drawString("分数:" + p1, 120, 40);
            //绘制关卡
            g.drawString("关卡:" + checkpoint, 250, 40);
            //绘制金币图片
            g.drawImage(money, 350, 15, money.getWidth() / 2, money.getHeight() / 2, null);
            //绘制金币数量
            g.drawString("金币数:" + sumBall, 400, 40);
            //绘制历史记录
            g.drawString("历史最高记录:" + maxScores, 550, 40);
            //绘制进度条的左括号
            g.drawImage(st, 10, 120, 10, 25, null);
            //调整字颜色
            g.setColor(Color.gray);
            //绘制灰色进度条
            for (int i = 0; i < 55; i++) {
                g.fillRect(30 + i * (10 + 5), 120, 10, 25);
            }
            //绘制蓝色进度条
            g.setColor(Color.cyan);
            for (int i = 0; i < amount && i < 55; i++) {
                g.fillRect(30 + i * (10 + 5), 120, 10, 25);
            }
            //绘制进度条右括号
            g.drawImage(ed, 50 + 54 * 15, 120, 10, 25, null);
            //绘制Boss
            if (checkpoint >=2 ){
                boss.draw(g);
            }
            //绘制飞机1
            if (!gameOver){
                plane.draw(g);
            }
            //绘制子弹   stars  判断子弹是否碰到小星星
            if(!stars){
                //绘制子弹1
                for (int i = 0; i < bullets.size(); i++) {
                    Bullet bullet = bullets.get(i);
                    bullet.draw(g);
                }
            }
            if(stars){
                //绘制子弹1
                for (int i = 0; i < bullets3.size(); i++) {
                    Bullet3 bullet3 = bullets3.get(i);
                    bullet3.draw(g);
                }
            }

            //绘制Boss 子弹
            for (int q = 0; q < bossBullets.size(); q++) {
                bossBullet bossBullet = bossBullets.get(q);
                bossBullet.draw(g);
            }


            //绘制球
            for (int j = 0; j < balls.size(); j++) {
                Ball ball = balls.get(j);
                ball.draw(g);
            }
            //绘制小星星
            for (int x = 0; x < picture3s.size(); x++) {
                Picture3 picture3 = picture3s.get(x);
                picture3.draw(g);
            }
            //绘制游戏结束提示
            if (gameOver ) {
                //设置画笔颜色
                g.setColor(Color.red);
                //设置透明度绘制红色矩形
                Graphics2D g2d1 = (Graphics2D) g;
                AlphaComposite alphaComposite1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
                g2d1.setComposite(alphaComposite1);
                g2d1.fillRect(0, 100, FinalCanstans.APP_WIDTH, 400);
                //透明度设置回来，然后设置成白色
                AlphaComposite alphaComposite11 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
                g2d1.setComposite(alphaComposite1);
                //重新设置字体样式和颜色
                g.setColor(Color.white);
                g.setFont(new Font("楷体", Font.BOLD, 25));
                g.drawString("游戏结束", 380, 250);
                g.setColor(Color.yellow);
                //绘制玩家1
                g.drawString("玩家:1",20,300);
                //绘制分数1
                g.drawString("分数:" + p1, 120, 300);
                //绘制关卡
                g.drawString("关卡:" + checkpoint, 250, 300);
                //绘制金币图片
                g.drawImage(money, 350, 275, money.getWidth() / 2, money.getHeight() / 2, null);
                //绘制金币数量
                g.drawString("金币数:" + sumBall, 400, 300);
                //绘制历史记录
                g.drawString("历史最高记录:" + maxScores, 550, 300);
                g.setColor(Color.white);
                g.drawString("{按F1重新开始游戏}", 350, 400);

            }
                    }
        if (paintFunction2){
            rec = true;
            //设置字体样式
            g.setFont(new Font("楷体", Font.ITALIC, 25));
            //设置字体颜色
            g.setColor(Color.white);
            //绘制玩家1
            g.drawString("玩家:1",20,40);
            //绘制玩家2
            g.drawString("玩家:2",20,80);
            //绘制分数1
            g.drawString("分数:" + p1, 120, 40);
            //绘制分数2
            g.drawString("分数:" + p2, 120, 80);
            //绘制关卡
            g.drawString("关卡:" + checkpoint, 250, 40);
            g.drawString("关卡:" + checkpoint, 250, 80);
            //绘制金币图片
            g.drawImage(money, 350, 15, money.getWidth() / 2, money.getHeight() / 2, null);
            g.drawImage(money, 350, 50, money.getWidth() / 2, money.getHeight() / 2, null);
            //绘制金币数量
            g.drawString("金币数:" + sumBall, 400, 40);
            g.drawString("金币数:" + sumBall2, 400, 80);
            //绘制历史记录
            g.drawString("历史最高记录:" + maxScores, 550, 40);
            g.drawString("历史最高记录:" + maxScores2, 550, 80);
            //绘制进度条的左括号
            g.drawImage(st, 10, 120, 10, 25, null);
            //调整字颜色
            g.setColor(Color.gray);
            //绘制灰色进度条
            for (int i = 0; i < 55; i++) {
                g.fillRect(30 + i * (10 + 5), 120, 10, 25);
            }
            //绘制蓝色进度条
            g.setColor(Color.cyan);
            for (int i = 0; i < amount && i < 55; i++) {
                g.fillRect(30 + i * (10 + 5), 120, 10, 25);
            }
            //绘制进度条右括号
            g.drawImage(ed, 50 + 54 * 15, 120, 10, 25, null);
            //绘制飞机1
            if (!gameOver){
                plane.draw(g);
            }
            //绘制飞机2
            if(!gameOver2){
                plane2.draw(g);
            }
            //绘制子弹1
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.draw(g);
            }
            //绘制子弹2
            for (int i = 0; i < bullets2.size(); i++) {
                Bullet bullet = bullets2.get(i);
                bullet.draw(g);
            }
            //绘制球
            for (int j = 0; j < balls.size(); j++) {
                Ball ball = balls.get(j);
                ball.draw(g);
            }

            //绘制游戏结束提示
            if (gameOver && gameOver2) {
                //设置画笔颜色
                g.setColor(Color.red);
                //设置透明度绘制红色矩形
                Graphics2D g2d1 = (Graphics2D) g;
                AlphaComposite alphaComposite1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
                g2d1.setComposite(alphaComposite1);
                g2d1.fillRect(0, 100, FinalCanstans.APP_WIDTH, 400);
                //透明度设置回来，然后设置成白色
                AlphaComposite alphaComposite11 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
                g2d1.setComposite(alphaComposite1);
                //重新设置字体样式和颜色
                g.setColor(Color.white);
                g.setFont(new Font("楷体", Font.BOLD, 25));
                g.drawString("游戏结束", 380, 250);
                g.setColor(Color.yellow);
                //绘制玩家1
                g.drawString("玩家:1",20,300);
                //绘制玩家2
                g.drawString("玩家:2",20,340);
                //绘制分数1
                g.drawString("分数:" + p1, 120, 300);
                //绘制分数2
                g.drawString("分数:" + p2, 120, 340);
                //绘制关卡
                g.drawString("关卡:" + checkpoint, 250, 300);
                g.drawString("关卡:" + checkpoint, 250, 340);
                //绘制金币图片
                g.drawImage(money, 350, 275, money.getWidth() / 2, money.getHeight() / 2, null);
                g.drawImage(money, 350, 310, money.getWidth() / 2, money.getHeight() / 2, null);
                //绘制金币数量
                g.drawString("金币数:" + sumBall, 400, 300);
                g.drawString("金币数:" + sumBall2, 400, 340);
                //绘制历史记录
                g.drawString("历史最高记录:" + maxScores, 550, 300);
                g.drawString("历史最高记录:" + maxScores2, 550, 340);

                g.setColor(Color.white);
                g.drawString("{按F1重新开始游戏}", 350, 400);

            }
        }
        }
    @SuppressWarnings("all")
    @Override
    public void run() {
        //按下相应的按钮程序才能开始启动
        if(paintFunction || paintFunction2){
            //判断进入的是否是单人模式
            if (paintFunction){
                if(!ban){
                if (!gameOver ) {
                    super.run();
                    //调用发射子弹的方法
                    if (!gameOver) {
                        plane.shootBullet();
                        plane.shootBullet2();
                        if(checkpoint >= 2){
                            boss.shootBullet0();
                        }
                    }
                    if(!paintFunction){
                        if (!gameOver2 ) {
                            plane2.shootBullet();
                        }
                    }
                    //移动大Boss图片
                    moveBoss();
                    //移动图片
                    movePicture();
                    //移动图片2
                    movePicture2();
                    //移动飞机1
                    movePlane();
                    //移动飞机2
                    movePlane2();
                    //创建小星星
                    creatStars();
                    //移动小星星
                    moveStars();
                    //判断小星星是否和飞机发生碰撞
                    ifStarsCrash();
                    //创建球
                    creatBall();
                    //移动球
                    moveBall();
                    //判断是否击中球
                    ifShootBall();
                    //判断飞机是否碰撞
                    if (!Continue) {
                        ifCrash();
                        ifCrashBoss();
                    }
                    //判断飞机是否碰撞
                    if (!Continue) {
                        ifCrash2();
                    }
                    // 重新绘制方法
                    repaint();
                }
                }

            }
            //判断进入的是否是双人模式
            if(paintFunction2){
                if(!ban){
                    //判断两架飞机是否同时坠毁
                    if (!gameOver || !gameOver2) {
                        super.run();
                        //调用发射子弹的方法
                        if (!gameOver) {
                            plane.shootBullet();
                        }
                        if(!paintFunction){
                            if (!gameOver2 ) {
                                plane2.shootBullet();
                            }
                        }
                        //移动图片
                        movePicture();
                        //移动图片2
                        movePicture2();
                        //移动飞机1
                        movePlane();
                        //移动飞机2
                        movePlane2();
                        //创建球
                        creatBall();
                        //移动球
                        moveBall();
                        //判断是否击中球
                        ifShootBall();
                        //判断飞机是否碰撞
                        if (!Continue) {
                            ifCrash();
                        }
                        //判断飞机是否碰撞
                        if (!Continue) {
                            ifCrash2();
                        }
                        // 重新绘制方法
                        repaint();
                    }
                }
            }
        }
    }

    //判断Boss的子弹是否和飞机碰撞
    private void ifCrashBoss() {
        //遍历每一给子弹
        for (int i = bossBullets.size() - 1; i >= 0; i--) {
            bossBullet bossBullet = bossBullets.get(i);
            if(plane.ifCrashBossBullet(bossBullet)){
                bossBullets.remove(bossBullet);
                gameOver = true;
                bossBullets.clear();
            }

        }

    }

    private void moveBoss() {
        boss.moveBossToLeft();
    }

    //创建方法判断小星星是否和飞机发生碰撞
    private void ifStarsCrash() {
        for (int i = picture3s.size() - 1; i >= 0; i--) {
            Picture3 picture3 = picture3s.get(i);
            if (plane.ifCrashStars(picture3)) {
                //碰撞之后让小星消失
                picture3s.remove(picture3);
                stars = true;
            }
        }
    }

    //创建一个方法移动小星星
    private void moveStars() {
        for (int i = 0; i < picture3s.size(); i++) {
            Picture3 picture3 = picture3s.get(i);
            picture3.moveStart();
        }
    }

    //创建一个方法创建小星星
    int count1 = 0;
    private void creatStars() {
        count1++;
        if (count1 % 200 == 0) {
            picture3s.add(new Picture3());
            count1 = 0;
        }
    }

    // 创建方法使得飞机1移动
    private void movePlane() {
        if(l){plane.moveToLeft();}
        if (r){plane.moveToRight();}
        if(u){plane.moveToUp();}
        if (d){plane.moveToDown();}
    }
    //创建方法移动飞机2
    private void movePlane2() {
        if(l2){plane2.moveToLeft2();}
        if (r2){plane2.moveToRight2();}
        if(u2){plane2.moveToUp2();}
        if (d2){plane2.moveToDown2();}
    }

    //创建方法移动背景图片1
    private void movePicture() {
        picture.movePicture();
    }
    //创建方法移动背景图片2
    private void movePicture2() {
        picture2.movePicture2();
    }
    //创建方法判断飞机1和球发生碰撞
        private void ifCrash () {
            for (int i = balls.size() - 1; i >= 0; i--) {
                Ball ball = balls.get(i);
                if (plane.ifCrash(ball)) {
                    //碰撞之后让球消失
                    balls.remove(ball);
                    bullets.clear();
                    bullets3.clear();
                    bossBullets.clear();
                     gameOver = true;
                }
            }

        }
        //创建方法判断飞机2和球发生碰撞
        private void ifCrash2 () {
            for (int i = balls.size() - 1; i >= 0; i--) {
                Ball ball = balls.get(i);
                if (plane2.ifCrash2(ball)) {
                    balls.remove(ball);
                    bullets2.clear();
                    gameOver2 = true;
                }
            }

        }
@SuppressWarnings("all")

        //创建方法判断是否击中球
        private void ifShootBall () {
            //遍历数组获得每个球对象
            for (int i = balls.size() - 1; i >= 0; i--) {
//                System.out.println("总分:"+amount);
                Ball ball = balls.get(i);

                //判断第一种子弹和球碰撞
                if (!stars){
                    //遍历数组获得每个子弹对象
                    for (int j = bullets.size() - 1; j >= 0; j--) {
                    Bullet bullet = bullets.get(j);
                    //调用方法判断球和子弹是否碰撞
                    if (ball.shootby(bullet)) {
                        amount++;
                        //碰撞了子弹消失
                        bullets.remove(bullet);
                        //记录小球被击中之后的分数
                        p1++;
                        System.out.println("飞机1:"+p1);

                        //进度条的控制

                        if (amount % 55 == 0) {
                            //进度条增加
//                            num++;
                            //System.out.println("11111111111111111"+num);
                            //关卡数里增加
                            checkpoint++;
                            amount = 0;
                            //System.out.println("2222222222222222"+checkpoint);
                        }

                    }

                    if (ball.getCurrentBlood() <= 0) {
                        //移除球
                        balls.remove(ball);
                        //金币增加
                        sumBall++;
                    }
                    //判断血量是否为原来的一半
                    if (ball.getCurrentBlood() <= ball.getBlood() / 2) {
                        ArrayList<Ball> split = ball.split();
                        //移除原本的小球
                        balls.remove(ball);
                        //将新创建的两个小球放进之前的集合之中
                        balls.addAll(split);
                    }
                }
                }

                //判断第二种子弹和球碰撞
                if (stars){     //遍历数组获得每个子弹对象
                    for (int j = bullets3.size() - 1; j >= 0; j--) {
                        Bullet3 bullet3 = bullets3.get(j);
                        //调用方法判断球和子弹是否碰撞
                        if (ball.shootby2(bullet3)) {
                            amount++;
                            //碰撞了子弹消失
                            bullets.remove(bullet3);
                            //记录小球被击中之后的分数
                            p1++;
                            System.out.println("飞机1:"+p1);

                            //进度条的控制

                            if (amount % 55 == 0) {
                                //进度条增加
//                            num++;
                                //System.out.println("11111111111111111"+num);
                                //关卡数里增加
                                checkpoint++;
                                amount = 0;
                                //System.out.println("2222222222222222"+checkpoint);
                            }

                        }

                        if (ball.getCurrentBlood() <= 0) {
                            //移除球
                            balls.remove(ball);
                            //金币增加
                            sumBall++;
                        }
                        //判断血量是否为原来的一半
                        if (ball.getCurrentBlood() <= ball.getBlood() / 2) {
                            ArrayList<Ball> split = ball.split();
                            //移除原本的小球
                            balls.remove(ball);
                            //将新创建的两个小球放进之前的集合之中
                            balls.addAll(split);
                        }
                    }
                }

                //飞机2
                for (int j = bullets2.size() - 1; j >= 0; j--) {
                    Bullet bullet = bullets2.get(j);
                    //调用方法判断球和子弹是否碰撞
                    if (ball.shootby(bullet)) {
                        amount++;
                        //碰撞了子弹消失
                        bullets2.remove(bullet);
                        //记录小球被击中之后的分数
                        p2++;
                        //System.out.println("飞机2:"+sum);
/*                        //进度条的控制
                        amount =sum2 +sum ;
                        System.out.println("总分"+amount);*/
                        if (amount % 55 == 0) {
                            //进度条增加
//                            num++;
                            //System.out.println("11111111111111111"+num);
                            //关卡数里增加
                            checkpoint++;
                            amount = 0;
                            //System.out.println("2222222222222222"+checkpoint);
                        }


                    }


                    if (ball.getCurrentBlood() <= 0) {
                        //移除球
                        balls.remove(ball);
                        //金币增加
                        sumBall2++;
                    }
                }
                //判断血量是否为原来的一半
                if (ball.getCurrentBlood() <= ball.getBlood() / 2) {
                    ArrayList<Ball> split = ball.split();
                    //移除原本的小球
                    balls.remove(ball);
                    //将新创建的两个小球放进之前的集合之中
                    balls.addAll(split);
                }
            }

        }


        //定义一个计时器,控制球出现的速度
        int count = 0;

        //创建球方法
        private void creatBall(){
            count++;
            if (count % 20 == 0) {
//                if (balls.size() <= 4) {
                    balls.add(new Ball());
                    count = 0;
                }
            }

//        }

        //移动球方法
        private void moveBall () {
            for (int i = 0; i < balls.size(); i++) {
                Ball ball = balls.get(i);
                ball.move();//move 方法书写在Ball 类中
            }
        }

        //创建音乐播放方法
    private void playBGMusic() {
        MusicUtil.loopPlayMusic("8553.wav");
    }

    }

